package is.hi.soundboard;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Soundboard extends Activity implements OnClickListener {
	
	private static final String TAG = "Soundboard";
	
	private MediaPlayer player;
	
	private static Button bCsi;
	private static Button bMarch;
	private static Button bLawAndOrder;
	private static Button bWrong;
	private static Button bMmm;
	private static Button bNo;
	private static Button bRimshot;
	private static Button bWilhelm;
	private static Button bDrama;
	private static Button bSad;
	
	private static Button bStop;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        player = new MediaPlayer();
        
        // Get handle on buttons
        bCsi = (Button)this.findViewById(R.id.csi);
        bMarch = (Button)this.findViewById(R.id.vader);
        bLawAndOrder = (Button)this.findViewById(R.id.lawandorder);
        bWrong = (Button)this.findViewById(R.id.wrong);
        bMmm = (Button) this.findViewById(R.id.mmm);
        bNo = (Button) this.findViewById(R.id.no);
        bRimshot = (Button) this.findViewById(R.id.rimshot);
        bWilhelm = (Button) this.findViewById(R.id.wilhelm);
        bDrama = (Button) this.findViewById(R.id.drama);
        bSad = (Button) this.findViewById(R.id.sad);
        
        bStop = (Button)this.findViewById(R.id.stop);
        
        
        // Add listeners
        bCsi.setOnClickListener(this);
        bMarch.setOnClickListener(this);
        bLawAndOrder.setOnClickListener(this);
        bWrong.setOnClickListener(this);
        bMmm.setOnClickListener(this);
        bNo.setOnClickListener(this);
        bRimshot.setOnClickListener(this);
        bWilhelm.setOnClickListener(this);
        bDrama.setOnClickListener(this);
        bSad.setOnClickListener(this);
        
        bStop.setOnClickListener(this);
        
    }
    
    
    
    @Override
	protected void onPause() {
		super.onPause();
		this.resetPlayer();
	}



	public void resetPlayer() {
    	if (player != null) {
    		if (player.isPlaying())
    			player.stop();
			player.release();
			player = null;
		}	
    }
    
	public void onClick(View v) {
		
		resetPlayer();
		if (v == bStop) {
			return;
		}
		
		int resid;
		if (v == bCsi) {
			resid = R.raw.csi;
		}
		else if(v == bMarch) {
			resid = R.raw.imperial_march;
		}
		else if (v == bLawAndOrder) {
			resid = R.raw.lawandorder;
		}
		else if(v == bWrong) {
			resid = R.raw.wrong;
		}
		else if(v == bMmm) {
			resid = R.raw.mmm;
		}
		else if(v == bNo) {
			resid = R.raw.no;
		}
		else if(v == bRimshot) {
			resid = R.raw.rimshot;
		}
		else if(v == bWilhelm) {
			resid = R.raw.wilhelm;
		}
		else if(v == bDrama) {
			resid = R.raw.drama;
		}
		else if(v == bSad) {
			resid = R.raw.sad;
		}
		else {
			Log.d(TAG, "No resorce found for " + ((Button)v).getText().toString());
			 resid = R.raw.csi; //TODO make a default or should we do nothing maybe?
		}
		
		player = MediaPlayer.create(Soundboard.this, resid);
		player.start();
		player.setOnCompletionListener(new OnCompletionListener(){
            // @Override
            public void onCompletion(MediaPlayer p) {
            	player.release();
            	player = null;
            }
       }); 
		
		
	}
}